package net.mikoo.seals.api.model.use;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.mina.proxy.utils.StringUtilities;

import net.mikoo.seals.api.model.BaseResponse;
import net.mikoo.seals.common.enums.ApplyTypeEnum;
import net.mikoo.seals.common.enums.TakeStatusEnum;
import net.mikoo.seals.model.DevicesAct;
import net.mikoo.seals.model.DevicesTake;
import net.mikoo.seals.model.DevicesUsed;

public class GetWaitUseListResponse extends BaseResponse {

	private static final long serialVersionUID = -254233859134587352L;
	
	private List<WaitUseInfo> datas;

	public List<WaitUseInfo> getDatas() {
		return datas;
	}

	public void setDatas(List<WaitUseInfo> datas) {
		this.datas = datas;
	}
	public static GetWaitUseListResponse fromAct(List<DevicesAct> actList) {
		GetWaitUseListResponse result = new GetWaitUseListResponse();
		if(actList == null) {
			return result;
		}
		List<WaitUseInfo> waitUseList = new ArrayList<WaitUseInfo>();
		waitUseList.addAll(fromDevicesActList(actList));
		result.setDatas(waitUseList);
		return result;
	}	
	
	
	public static GetWaitUseListResponse fromTake(List<DevicesTake> takeList) {
		GetWaitUseListResponse result = new GetWaitUseListResponse();
		
		if(takeList == null) {
			return result;
		}
		List<WaitUseInfo> waitUseList = new ArrayList<WaitUseInfo>();
		waitUseList.addAll(fromDevicesUsedList(takeList));
		result.setDatas(waitUseList);
		return result;
	}
	
	private static List<WaitUseInfo> fromDevicesUsedList(List<DevicesTake> takeList){
		List<WaitUseInfo> result = new ArrayList<WaitUseInfo>();
		if(takeList == null || takeList.isEmpty()) {
			return result;
		}
		for(DevicesTake devicesTake : takeList) {
			if(isInvalid(devicesTake)) {
				continue;
			}
			
			WaitUseInfo waitUseInfo = new WaitUseInfo();
			waitUseInfo.setId(devicesTake.getDevicesTakeId());
			waitUseInfo.setType(ApplyTypeEnum.TAKE_APPLY.getCode());
			
			TakeStatusEnum takeStatus = TakeStatusEnum.getByCode(devicesTake.getTakeStatus());
			
			if(takeStatus != null) {
				waitUseInfo.setStatus(takeStatus.getCode());
				waitUseInfo.setStatusName(takeStatus.getMessage());
			}
			
			String sealName = devicesTake.getSeal().getSealName();
			if(StringUtils.isNotBlank(sealName)) {
				waitUseInfo.setSealName(sealName);
				waitUseInfo.setIconText(sealName.substring(0, sealName.length() < 2 ? 1 : 2));
			}
			
			waitUseInfo.setCustos(devicesTake.getSeal().getCustosName());
			
			result.add(waitUseInfo);
		}
		return result;
	}
	
	private static boolean isInvalid(DevicesTake devicesTake) {
		if(devicesTake == null) {
			return true;
		}
		
		long currentTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN).toInstant(ZoneOffset.of("+8")).toEpochMilli();
		if(devicesTake.getEndDate() != null && devicesTake.getEndDate().getTime() < currentTime && !TakeStatusEnum.WAIT_RETURN.getCode().equals(devicesTake.getTakeStatus())) {
			return true;
		}
		
		return false;
	}
	
	private static List<WaitUseInfo> fromDevicesActList(List<DevicesAct> actList){
		List<WaitUseInfo> result = new ArrayList<WaitUseInfo>();
		if(actList == null || actList.isEmpty()) {
			return result;
		}
		
		for(DevicesAct devicesAct : actList) {
			if(devicesAct == null) {
				continue;
			}
			WaitUseInfo waitUseInfo = new WaitUseInfo();
			waitUseInfo.setId(devicesAct.getDevicesActId());
			waitUseInfo.setType(ApplyTypeEnum.USED_APPLY.getCode());
			waitUseInfo.setTotal(devicesAct.getTotalTimes());
			waitUseInfo.setSurplus(devicesAct.getTotalTimes() - devicesAct.getUseTimes());
			
			String sealName = devicesAct.getSeal().getSealName();
			if(StringUtils.isNotBlank(sealName)) {
				waitUseInfo.setSealName(sealName);
				waitUseInfo.setIconText(sealName.substring(0, sealName.length() < 2 ? 1 : 2));
			}
			waitUseInfo.setCustos(devicesAct.getSeal().getCustosName());
			result.add(waitUseInfo);
		}
		
		return result;
	}
}
